# coding:utf-8
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow


class Calc(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi('calc.ui', self)
        self.Text.setText('0')
        self.x = None
        self.y = None
        self.z = None
        self.operate = None
        self.isdot = None

        button = [self.pushButton0, self.pushButton1, self.pushButton2, self.pushButton3, self.pushButton4, self.pushButton5, self.pushButton6, self.pushButton7, self.pushButton8,
                  self.pushButton9, self.pushButton_add, self.pushButton_clear, self.pushButton_div, self.pushButton_dot, self.pushButton_equal, self.pushButton_mul, self.pushButton_sub]
        map(lambda x: x.clicked.connect(self.f), button)

    def calculate(self):
        '''
        计算结果。
        '''
        if self.x and self.operate and self.y:
            if float(self.y)==0  and self.operate == '/':
                self.Text.setText('除数不能为零')
            else:
                self.yunsuan = '%s%s%s' % (
                    float(self.x), self.operate, float(self.y))
                self.Text_2.setText(self.yunsuan)
                self.result = str(eval(self.yunsuan))
                self.Text.setText(self.result)
                self.x = self.result
                self.z = self.y
                self.y = None

        elif self.x and self.operate and self.z:
            self.yunsuan = '%s%s%s' % (
                float(self.x), self.operate, float(self.z))
            self.Text_2.setText(self.yunsuan)
            self.result = str(eval(self.yunsuan))
            self.Text.setText(self.result)
            self.x = self.result

        elif self.x and self.operate and self.y is None:
            if self.operate == '/':
                self.Text.setText('除数不能为零')
            else:
                self.z = '0'
                self.yunsuan = '%s%s%s' % (float(self.x), self.operate, float(self.z))
                self.Text_2.setText(self.yunsuan)
                self.result = str(eval(self.yunsuan))
                self.Text.setText(self.result)
                self.yunsuan = '%s%s%s' % (float(self.x), self.operate, float(self.z))
        elif self.operate and self.x is None:
            self.x = '0'
            self.yunsuan = '%s%s%s' % (
                float(self.x), self.operate, float(self.y))        
            self.Text_2.setText(self.yunsuan)
            self.result = str(eval(self.yunsuan))
            self.Text.setText(self.result)
            self.x = self.result
            self.z = self.y
            self.y = None

    def number_1(self):
        if sender == '.' and self.isdot is None:
            if self.x is None:
                self.x = '0.'
                self.isdot = 1
            else:
                self.x += '.'
                self.isdot = 1
        elif sender != '.' and self.x is None:
            self.x = sender
        elif sender != '.':
            self.x += sender
        self.Text.setText(self.x)

    def number_2(self):
        if sender == '.' and self.isdot is None:
            if self.y is None:
                self.y = '0.'
                self.isdot = 1
            else:
                self.y += '.'
                self.isdot = 1
        elif sender != '.' and self.y is None:
            self.y = sender
        elif sender != '.':
            self.y += sender
        self.Text.setText(self.y)

    def operation(self):
        if self.x and self.y and self.operate:
            self.calculate()
            self.y = None
            self.operate = sender
            self.yunsuan += sender
            self.Text_2.setText(self.yunsuan)

        else:
            self.operate = sender
            self.Text.setText(sender)

    def clear(self):
        self.Text.setText('0')
        self.Text_2.setText('')
        self.x = None
        self.y = None
        self.z = None
        self.operate = None
        self.isdot = None

    def f(self):
        global sender
        sender = self.sender().text()
        if sender in '0123456789.' and self.operate is None:
            self.number_1()

        elif sender in '0123456789.' and self.operate:
            self.number_2()

        elif sender in '+-*/':
            if self.x is None:
                self.x = '0'
            self.isdot = None
            self.operation()

        elif sender == '=':
            self.calculate()

        elif sender == 'C':
            self.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    calc = Calc()
    calc.show()
    app.exec_()
